import * as Cesium from "cesium";

// 距离测量类
export default class MeasureDistance {
  constructor(viewer) {
    this.viewer = viewer;
    this.initEvents();
    this.positions = [];
    this.tempPositions = [];
    this.vertexEntities = [];
    this.labelEntity = undefined;
    this.measureDistance = 0; // 测量结果
  }

  // 初始化事件
  initEvents() {
    this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
    this.MeasureStartEvent = new Cesium.Event(); // 开始事件
    this.MeasureEndEvent = new Cesium.Event(); // 结束事件
  }

  // 激活测量工具
  activate() {
    this.deactivate();
    this.registerEvents(); // 注册鼠标事件
    this.viewer.enableCursorStyle = false;
    this.viewer._element.style.cursor = "default";
    this.isMeasure = true;
    this.measureDistance = 0;
  }

  // 禁用测量工具
  deactivate() {
    if (!this.isMeasure) return;
    this.unRegisterEvents();
    this.viewer._element.style.cursor = "pointer";
    this.viewer.enableCursorStyle = true;
    this.isMeasure = false;
    this.tempPositions = [];
    this.positions = [];
  }

  // 清空绘制
  clear() {
    this.viewer.entities.remove(this.lineEntity);
    this.lineEntity = undefined;
    this.vertexEntities.forEach((item) => {
      this.viewer.entities.remove(item);
    });
    this.vertexEntities = [];
  }

  // 创建线对象
  createLineEntity() {
    this.lineEntity = this.viewer.entities.add({
      polyline: {
        positions: new Cesium.CallbackProperty((e) => {
          return this.tempPositions;
        }, false),
        width: 2,
        material: Cesium.Color.YELLOW,
        depthFailMaterial: Cesium.Color.YELLOW,
      },
    });
  }

  // 创建线节点
  createVertex() {
    let vertexEntity = this.viewer.entities.add({
      position: this.positions[this.positions.length - 1],
      id: "MeasureDistanceVertex" + this.positions[this.positions.length - 1],
      type: "MeasureDistanceVertex",
      label: {
        text: spaceDistance(this.positions) + "米",
        scale: 0.5,
        font: "normal 24px MicroSoft YaHei",
        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 5000),
        scaleByDistance: new Cesium.NearFarScalar(1000, 1, 3000, 0.4),
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
        pixelOffset: new Cesium.Cartesian2(0, -30),
        outlineWidth: 9,
        outlineColor: Cesium.Color.WHITE,
      },
      point: {
        color: Cesium.Color.FUCHSIA,
        pixelSize: 8,
        disableDepthTestDistance: 500,
      },
    });
    this.vertexEntities.push(vertexEntity);
  }

  // 注册鼠标事件
  registerEvents() {
    this.leftClickEvent();
    this.rightClickEvent();
    this.mouseMoveEvent();
  }

  // 左键点击事件
  leftClickEvent() {
    this.handler.setInputAction((e) => {
      this.viewer._element.style.cursor = "default";
      let position = this.viewer.scene.pickPosition(e.position);
      if (!position) {
        const ellipsoid = this.viewer.scene.globe.ellipsoid;
        position = this.viewer.scene.camera.pickEllipsoid(
          e.position,
          ellipsoid
        );
      }
      if (!position) return;
      this.positions.push(position);
      if (this.positions.length == 1) {
        this.createLineEntity();
        this.createStartEntity();
        return;
      }
      this.createVertex();
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  // 鼠标移动事件
  mouseMoveEvent() {
    this.handler.setInputAction((e) => {
      if (!this.isMeasure) return;
      this.viewer._element.style.cursor = "default";
      let position = this.viewer.scene.pickPosition(e.endPosition);
      if (!position) {
        position = this.viewer.scene.camera.pickEllipsoid(
          e.startPosition,
          this.viewer.scene.globe.ellipsoid
        );
      }
      if (!position) return;
      this.handleMoveEvent(position);
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  // 处理鼠标移动
  handleMoveEvent(position) {
    if (this.positions.length < 1) return;
    this.tempPositions = this.positions.concat(position);
  }

  // 右键事件
  rightClickEvent() {
    this.handler.setInputAction((e) => {
      if (!this.isMeasure || this.positions.length < 1) {
        this.deactivate();
        this.clear();
      } else {
        this.createEndEntity();
        this.lineEntity.polyline = {
          positions: this.positions,
          width: 2,
          material: Cesium.Color.YELLOW,
          depthFailMaterial: Cesium.Color.YELLOW,
        };
        this.measureEnd();
      }
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  // 测量结束
  measureEnd() {
    this.deactivate();
    this.MeasureEndEvent.raiseEvent(this.measureDistance); // 触发结束事件 传入结果
  }

  // 解除鼠标事件
  unRegisterEvents() {
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }
}
